import time
from selenium import webdriver
from selenium.common.exceptions import InvalidArgumentException, WebDriverException, NoSuchElementException

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

class Base:
    def __init__(self,browse):
        if browse.lower() == 'chrome':
            self.driver = webdriver.Chrome()
            self.driver.maximize_window()
        elif browse == 'firefox':
            self.driver = webdriver.Firefox()
            self.driver.maximize_window()
        elif browse == 'edge':
            self.driver = webdriver.Edge()
            self.driver.maximize_window()
        else:
            print('不支持该浏览器')
            self.driver = None
    def get(self,url):
        try:
            self.driver.get(url)
            time.sleep(2)
        except:
            print('输入地址不正确')
    def find_element(self,locator):
        try:
            ele=self.driver.find_element(*locator)
            print(ele.get_attribute('outerHTML'))
            return ele
        except:
            print('元素定位失败')
    def click(self,locator):
        element = self.find_element(locator)
        if element:
            element.click()
    def get_attribute_value(self,locator,*attribute):
        ele = self.find_element(locator)
        valuelist=[]
        if bool(attribute):
            for i in attribute:
                valus = ele.get_attribute(i)
                valuelist.append({i:f'{valus}'})
            return valuelist
        else:
            print('没输入属性')
    def send_keys(self,locator,context):
        ele = self.find_element(locator)
        try:
            if ele:
                ele.send_keys(context)
        except:
            print('该元素不能send_keys')
    def page_control(self,controll='refresh'):
        if controll =='refresh':
            self.driver.refresh()
        elif controll =='b':
            self.driver.back()
        elif controll =='f':
            self.driver.forward()
        elif controll =='c':
            self.driver.close()
    def quit(self,seconds=0):
        time.sleep(seconds)
        self.driver.quit()
if __name__ == '__main__':
    case = Base('Chrome')
    case.get('https://www.baidu.com')
    case.send_keys(('TAG_NAME','a'),'caixukun')
    time.sleep(2)
    print(case.get_attribute_value((By.ID,'kw'),'name','id'))
    case.quit()





